class Solution {
public:
    using ll=long long;
    long long maxSubarraySum(vector<int>& nums, int k) {
        vector<ll> min_sum(k, LLONG_MAX>>1);
        min_sum.back()=0;

        ll ans=LLONG_MIN, sum=0;
        for(int j=0; j<nums.size(); j++) {
            sum+=nums[j];
            int i=j%k;
            ans=max(ans, sum-min_sum[i]);
            min_sum[i]=min(min_sum[i], sum); //维护前缀和
        }
        return ans;
    }
};